<template>
  <div class="stock-list">
    <el-card>
      <!-- 搜索栏 -->
      <el-form :inline="true" :model="searchForm" class="search-form">
        <el-form-item label="药品名称">
          <el-input v-model="searchForm.name" placeholder="请输入药品名称" />
        </el-form-item>
        <el-form-item label="库存状态">
          <el-select v-model="searchForm.status" placeholder="请选择状态">
            <el-option label="库存充足" value="normal" />
            <el-option label="库存不足" value="low" />
            <el-option label="库存告急" value="critical" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">搜索</el-button>
          <el-button @click="resetSearch">重置</el-button>
        </el-form-item>
      </el-form>

      <!-- 工具栏 -->
      <div class="toolbar">
        <el-button type="primary" @click="handleStockIn">入库</el-button>
        <el-button type="warning" @click="handleStockOut">出库</el-button>
      </div>

      <!-- 数据表��� -->
      <el-table
        v-loading="loading"
        :data="tableData"
      >
        <el-table-column prop="code" label="药品编码" width="120" />
        <el-table-column prop="name" label="药品名称" width="150" />
        <el-table-column prop="specification" label="规格" width="120" />
        <el-table-column prop="manufacturer" label="生产厂家" width="200" />
        <el-table-column prop="stock" label="当前库存" width="100" />
        <el-table-column prop="minStock" label="最低库存" width="100" />
        <el-table-column prop="status" label="状态" width="100">
          <template #default="{ row }">
            <el-tag :type="getStatusType(row.status)">
              {{ getStatusText(row.status) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="lastStockTime" label="最后操作时间" width="160" />
        <el-table-column label="操作" fixed="right" width="200">
          <template #default="{ row }">
            <el-button link type="primary" @click="handleStockIn(row)">入库</el-button>
            <el-button link type="warning" @click="handleStockOut(row)">出库</el-button>
            <el-button link type="primary" @click="handleRecord(row)">记录</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <div class="pagination">
        <el-pagination
          v-model:current-page="currentPage"
          v-model:page-size="pageSize"
          :total="total"
          :page-sizes="[10, 20, 50, 100]"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>

    <!-- 入库/出库对话框 -->
    <el-dialog
      v-model="dialogVisible"
      :title="dialogType === 'in' ? '入库' : '出库'"
      width="500px"
    >
      <el-form
        ref="formRef"
        :model="form"
        :rules="rules"
        label-width="100px"
      >
        <el-form-item label="药品名称">
          <span>{{ currentMedicine?.name }}</span>
        </el-form-item>
        <el-form-item label="当前库存">
          <span>{{ currentMedicine?.stock }}</span>
        </el-form-item>
        <el-form-item label="数量" prop="quantity">
          <el-input-number v-model="form.quantity" :min="1" />
        </el-form-item>
        <el-form-item label="批次" prop="batch">
          <el-input v-model="form.batch" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input
            v-model="form.remark"
            type="textarea"
            :rows="3"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="handleSubmit">确定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import type { FormInstance } from 'element-plus'

// 搜索表单
const searchForm = reactive({
  name: '',
  status: ''
})

// 表格数据
const loading = ref(false)
const tableData = ref([
  {
    code: 'M001',
    name: '阿莫西林胶囊',
    specification: '0.25g*24粒',
    manufacturer: '哈药集团制药总厂',
    stock: 1000,
    minStock: 100,
    status: 'normal',
    lastStockTime: '2024-03-18 10:00:00'
  }
])

// 分页
const currentPage = ref(1)
const pageSize = ref(10)
const total = ref(100)

// 对话框
const dialogVisible = ref(false)
const dialogType = ref('in')
const formRef = ref<FormInstance>()
const currentMedicine = ref<any>(null)
const form = reactive({
  quantity: 1,
  batch: '',
  remark: ''
})

// 表单验证规则
const rules = {
  quantity: [{ required: true, message: '请输入数量', trigger: 'blur' }],
  batch: [{ required: true, message: '请输入批次', trigger: 'blur' }]
}

// 获取状态类型
const getStatusType = (status: string) => {
  const map: Record<string, string> = {
    normal: 'success',
    low: 'warning',
    critical: 'danger'
  }
  return map[status] || 'info'
}

// 获取状态文本
const getStatusText = (status: string) => {
  const map: Record<string, string> = {
    normal: '库存充足',
    low: '库存不足',
    critical: '库存告急'
  }
  return map[status] || '未知'
}

// 搜索
const handleSearch = () => {
  // TODO: 实现搜索逻辑
  console.log('search:', searchForm)
}

// 重置搜索
const resetSearch = () => {
  searchForm.name = ''
  searchForm.status = ''
  handleSearch()
}

// 入库
const handleStockIn = (row?: any) => {
  dialogType.value = 'in'
  dialogVisible.value = true
  currentMedicine.value = row
  // 重置表单
  Object.keys(form).forEach(key => {
    form[key] = ''
  })
  form.quantity = 1
}

// 出库
const handleStockOut = (row?: any) => {
  dialogType.value = 'out'
  dialogVisible.value = true
  currentMedicine.value = row
  // 重置表单
  Object.keys(form).forEach(key => {
    form[key] = ''
  })
  form.quantity = 1
}

// 查看记录
const handleRecord = (row: any) => {
  // TODO: 实现查看记录逻辑
  console.log('record:', row)
}

// 表单提交
const handleSubmit = async () => {
  if (!formRef.value) return
  await formRef.value.validate((valid) => {
    if (valid) {
      // TODO: 实现提交逻辑
      ElMessage.success(dialogType.value === 'in' ? '入库成功' : '出库成功')
      dialogVisible.value = false
    }
  })
}

// 分页大小变化
const handleSizeChange = (val: number) => {
  pageSize.value = val
  handleSearch()
}

// 页码变化
const handleCurrentChange = (val: number) => {
  currentPage.value = val
  handleSearch()
}
</script>

<style scoped>
.stock-list {
  padding: 20px;
}
.search-form {
  margin-bottom: 20px;
}
.toolbar {
  margin-bottom: 20px;
}
.pagination {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}
</style> 